package cn.itcast.listenner;/*
 * @author: Hayabusa
 * @date: 2022/2/1 21:21
 * @Description: 监听消息
 * @Email:2682557218@qq.com
 */

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class SpringRabbitListener {
    /**
     * 简单消息处理
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String message) {
//        简单消息处理
        System.out.println("监听到的消息是" + message);

    }

    /**
     * 工作消息处理 两个效率不一样
     *
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
        System.out.println("spring 消费者1接收到消息：【" + msg + "】");
        Thread.sleep(25);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage2(String msg) throws InterruptedException {
        System.err.println("spring 消费者2接收到消息：【" + msg + "】");
        Thread.sleep(100);
    }

    /**
     * 发布订阅 ：广播消息处理
     */
//    队列1
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) {
        System.out.println("监听到的fanout.queue1消息是" + message);

    }

    //    队列2
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) {
        System.out.println("监听到的fanout.queue2消息是" + message);

    }

    /**
     * 发布订阅：路由模式
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue1"),
                    exchange = @Exchange(name = "hayabusa.direct", type = ExchangeTypes.DIRECT),
                    key = {"red", "yellow"}
            )
    )
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到Direct消息:[" + msg + "]");
    }

    /**
     * 发布订阅：路由模式
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue2"),
                    exchange = @Exchange(name = "hayabusa.direct", type = ExchangeTypes.DIRECT),
                    key = {"blue", "yellow"}
            )
    )
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到Direct消息:[" + msg + "]");
    }

    /**
     * 发布订阅：主题`模式
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue1"),
                    exchange = @Exchange(name = "hayabusa.Topic", type = ExchangeTypes.TOPIC),
                    key = "china.#"
            )
    )
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1接收到Topic消息:[" + msg + "]");
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue2"),
                    exchange = @Exchange(name = "hayabusa.Topic", type = ExchangeTypes.TOPIC),
                    key = "#.news"
            )
    )
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者2接收到Topic消息:[" + msg + "]");
    }

    /**
     * json对象接受
     * @param msg json类型
     */

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, Object> msg) {
        System.out.println("收到的消息：【" + msg + "】");
    }
}
